I have a subtitle file in srt format showing the time i  hh:mm:ss format. I have a telemetry file in csv format which gives time and several colums of data. I would to read the csv file ;pick up a  txt file list of a few parameter names; the original subtile file and for each entry add paramter values to time ;making a new subtitle file


ChatGPT said:
Perfect — you're looking to enrich your existing .srt subtitle file by injecting telemetry parameter values from a .csv file, for each subtitle entry, using a list of chosen parameters.

✅ Input Files
You have:

telemetry.csv
Contains: time (in seconds or formatted), plus several telemetry fields (e.g., altitude, speed, etc.)

params.txt
A plain list of parameters (e.g.):

nginx
Copy
Edit
altitude
speed
battery
original.srt
A valid subtitle file with timings in hh:mm:ss,ms format.

✅ Goal
For each subtitle entry, add telemetry values corresponding to the start time of that subtitle (or closest match), like this:

makefile
Copy
Edit
1
00:00:01,000 --> 00:00:03,000
Time: 00:00:01
Altitude: 105.2 m
Speed: 17.6 km/h
Battery: 88%
🐍 Python Script to Do It
python
Copy
Edit
import pandas as pd
import pysrt
from datetime import timedelta

# --- Load data ---
telemetry = pd.read_csv('telemetry.csv')  # assumes 'time' in seconds
telemetry = telemetry.sort_values('time')

# Convert time column to timedelta for matching
telemetry['time'] = pd.to_timedelta(telemetry['time'], unit='s')

# Load list of parameters to include
with open('params.txt') as f:
    params = [line.strip() for line in f if line.strip()]

# Load subtitle file
subs = pysrt.open('original.srt')

# --- Build new subtitle file ---
new_subs = pysrt.SubRipFile()

for sub in subs:
    # Get start time as timedelta
    t = timedelta(
        hours=sub.start.hours,
        minutes=sub.start.minutes,
        seconds=sub.start.seconds,
        milliseconds=sub.start.milliseconds
    )

    # Find closest telemetry row
    idx = telemetry['time'].sub(t).abs().idxmin()
    row = telemetry.loc[idx]

    # Build subtitle text
    time_str = f"Time: {str(t).split('.')[0]}"
    values = [f"{param.capitalize()}: {row[param]}" for param in params if param in telemetry.columns]
    new_text = "\n".join([time_str] + values)

    # Create new subtitle
    new_sub = pysrt.SubRipItem(index=sub.index, start=sub.start, end=sub.end, text=new_text)
    new_subs.append(new_sub)

# --- Save updated subtitle file ---
new_subs.save('enriched_subtitles.srt', encoding='utf-8')

print("✅ Enriched subtitle file saved as: enriched_subtitles.srt")
🔍 Notes
Telemetry time and subtitle start times must be in the same scale (typically seconds since start).

Adjust formatting (e.g., units or rounding) if needed.

If your CSV time is in hh:mm:ss format instead of seconds, I can help modify it.

Would you like the script to format specific parameters (e.g., show altitude in meters, battery as %, or speed in km/h)?









